package com.yogee.topseller.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.bean.AddFastQueryBean;
import com.yogee.topseller.bean.ModifyFastQueryLabelBean;
import com.yogee.topseller.bean.ModifyNameBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.UpdateNameFragment;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.FlowLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 闪搜全部标签
 *
 * @auathor ${XiaoJianghong}
 * @date 2017/7/17.
 */

public class FlashAllLableActivity extends HttpActivity implements UpdateNameFragment.UpdateNameListener, FlowLayout.OnDeleteClickListener, FlowLayout.OnAddClickListener {

    @BindView(R.id.flash_details_title_tv)
    TextView titleTv;
    @BindView(R.id.flash_all_defout_fl)
    FlowLayout defoutFl;
    @BindView(R.id.flash_all_hot_lable_fl)
    FlowLayout hotLableFl;
    private List<AddFastQueryBean.LabelListBean> labelListBeen;
    private String flashId;
    private String name;
    private UpdateNameFragment fragment;
    private String labels;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_flash_all_lable;
    }

    @Override
    protected void initView() {
        flashId = getIntent().getStringExtra("flashId");
        defoutFl.setOnDeleteClickListener(this);
        hotLableFl.setOnAddClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        addFastQueryPage(AppUtil.getUserId(this));
    }

    @OnClick({R.id.flash_details_back_iv, R.id.flash_details_right_iv, R.id.flash_all_sure_rl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.flash_details_back_iv:
                Intent intent = new Intent();
                intent.putExtra("flashId", flashId);
                setResult(101, intent);
                finish();
                break;
            case R.id.flash_details_right_iv://修改名称
                fragment = new UpdateNameFragment();
                fragment.show(getFragmentManager(), "");
                fragment.setUpdateNameListener(this);
                break;
            case R.id.flash_all_sure_rl://闪搜
                if (labelListBeen.size() > 0)
                    startActivity(new Intent(this, BeginQueryActivity.class).putExtra("id", flashId));
                else
                    ToastUtils.showToast(this, "请选择标签");
                break;
        }
    }

    @Override
    public void onUpdateNameCLick(String name) {//修改名字
        modifyName(flashId, name);
    }

    @Override
    public void onDeleteClick(AddFastQueryBean.LabelListBean bean) {
        if (labelListBeen.contains(bean)) {
            labelListBeen.remove(bean);
            defoutFl.setDeleteDatas(labelListBeen);
            String labels = "";
            if (labelListBeen.size() > 0)
                for (AddFastQueryBean.LabelListBean bean1 : labelListBeen) {
                    labels += "," + bean1.getId();
                }
            if (!labels.isEmpty())
                labels = labels.substring(1);
            modifyFastQuery(AppUtil.getUserId(this), labels, name, flashId);
        }
    }

    @Override
    public void onAddClick(AddFastQueryBean.FootListBean footListBean) {
        AddFastQueryBean.LabelListBean ListBean = new AddFastQueryBean.LabelListBean();
        ListBean.setName(footListBean.getName());
        ListBean.setId(footListBean.getId());

        boolean isHas = false;

        for (int i = 0; i < labelListBeen.size(); i++) {
            if (labelListBeen.get(i).getId().equals(footListBean.getId())) {
                isHas = true;
                ToastUtils.showToast(FlashAllLableActivity.this, "标签已经存在");
                break;
            }
        }
        if (!isHas) {
            labelListBeen.add(ListBean);
            defoutFl.setDeleteDatas(labelListBeen);
            labels = "";
            if (labelListBeen.size() > 0)
                for (AddFastQueryBean.LabelListBean bean1 : labelListBeen) {
                    labels += "," + bean1.getId();
                }
            labels = labels.substring(1);
            modifyFastQuery(AppUtil.getUserId(this), labels, name, flashId);
        }
    }

    /**
     * 添加闪搜
     *
     * @param userId
     */
    private void addFastQueryPage(String userId) {
        HttpManager.getInstance().addFastQueryPage(userId, "1", flashId).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<AddFastQueryBean>() {
            @Override
            public void onNext(AddFastQueryBean o) {
                name = o.getName();
                titleTv.setText(o.getName());
                labelListBeen = o.getLabelList();
                defoutFl.setDeleteDatas(o.getLabelList());
//                hotLableFl.setHotDatas(o.getFootList());
//                hotLableFl.setHotDatasShow(o.getFootList(),o.getLabelList());
            }
        }, this));
    }

    /**
     * 更改闪搜名字
     *
     * @param flashId
     * @param name
     */
    private void modifyName(String flashId, String name) {
        HttpManager.getInstance().modifyName(flashId, name).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ModifyNameBean>() {
            @Override
            public void onNext(ModifyNameBean o) {
                titleTv.setText(o.getName());
                fragment.dismiss();
            }
        }, this));
    }

    /**
     * 实时保存闪搜
     *
     * @param userId
     * @param labels
     * @param name
     * @param flashId
     */
    private void modifyFastQuery(final String userId, String labels, String name, final String flashId) {
        HttpManager.getInstance().modifyFastQueryLabel(userId, labels, name, flashId).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ModifyFastQueryLabelBean>() {
            @Override
            public void onNext(ModifyFastQueryLabelBean o) {
                FlashAllLableActivity.this.flashId = o.getQueryId();
                addFastQueryPage(userId);
            }
        }, this));
    }
}